//
//  HMHomeOriginalView.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 2017/2/4.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit


class HMHomeOriginalView: UIView {

    
    let gap: Int = 10
    var originalViewBottomConstraint: Constraint?
    
    var statusModel: HMStatusViewModel? {
        didSet{
            
            //昵称
            nameLabel.text = statusModel?.homeModle?.user?.name
            //头像
            iconImage.hm_setImage(imgString: statusModel?.homeModle?.user?.profile_image_url)
            /// 微博内容
            contentLabel.text = statusModel?.homeModle?.text
            /// 微博会员等级
            membershipImageView.image = statusModel?.memberImage

            /// 微博认证图标
            avatarImageView.image = statusModel?.avatarImage
            
            //给collectionView赋值
            pictureView.pictureUrls = statusModel?.homeModle?.pic_urls
            
            originalViewBottomConstraint?.uninstall()
            // 有配图
            if let picUrls = statusModel?.homeModle?.pic_urls, picUrls.count > 0{
                
                pictureView.pictureUrls = picUrls

                snp_makeConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint

                }
                
                pictureView.isHidden = false
            }else {
                snp_makeConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint

                }
                pictureView.isHidden = true
            }
            /// 数据 时间
            timeLabel.text = statusModel?.dealSinaTime

            /// 微博来源
            sourceLabel.text = statusModel?.sinaSource
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() -> Void {
        
        /// 头像
        addSubview(iconImage)
        /// 昵称
        addSubview(nameLabel)
        /// 会员等级
        addSubview(membershipImageView)
        /// 时间
        addSubview(timeLabel)
        /// 来源
        addSubview(sourceLabel)
        /// 认证
        addSubview(avatarImageView)
        /// 微博内容
        addSubview(contentLabel)
        /// 微博内容图片
        addSubview(pictureView)
        
        iconImage.snp_makeConstraints { (make) in
            
            make.top.left.equalTo(self).offset(gap)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        
        nameLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(iconImage.snp_right).offset(gap)
            make.top.equalTo(iconImage)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            
            make.left.equalTo(nameLabel.snp_right).offset(gap)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconImage.snp_right).offset(gap)
            make.bottom.equalTo(iconImage.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(gap)
            make.centerY.equalTo(timeLabel)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImage.snp_bottom).offset(gap)
            make.left.equalTo(self).offset(gap)
            make.width.equalTo(SCREENWIDTH - CGFloat(2 * gap))
//            originalViewBottomConstraint = make.bottom.equalTo(self.snp_bottom).offset(-gap).constraint
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(gap)
            make.left.equalTo(gap)
           //方式1
//                          originalViewBottomConstraint = make.bottom.equalTo(self.snp_bottom).offset(-gap).constraint
        }
        
        //设置底部约束
        snp_makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(gap).constraint
        }

        
        
    }
    
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    
    private lazy var nameLabel: UILabel = UILabel(textSize: midTextFont, color: UIColor.black)
    
    
    private lazy var membershipImageView:UIImageView = UIImageView(imgName: "common_icon_membership")

    private lazy var timeLabel: UILabel = UILabel(textSize: smallTextFont, color: themeColor)
    
    private lazy var sourceLabel: UILabel = UILabel(textSize: smallTextFont, color: themeColor)
    
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_enterprise_vip")
    
    /// 微博内容
    private lazy var contentLabel: UILabel = {
        
        let lab = UILabel(textSize: midTextFont, color: UIColor.black)
        lab.numberOfLines = 0
        
        return lab
    }()
    /// 微博内容图片
    private lazy var pictureView:HMStatusPictureView = HMStatusPictureView()
}
